#include "RsGisRpcMaster.h"

#include <QVariant>
#include <QDateTime>
#include <QTime>
#include "maia/maiaXmlRpcServer.h"

#include "ExtensionManager.h"
#include "ExtensionSession.h"

#include "XmlRpcMaster.h"
#include "XmlRpcClient.h"
#include "XmlRpcServer.h"

RsGisRpcMaster::RsGisRpcMaster(TaskModel* model, QObject* parent) : XmlRpcService(parent)
{
	taskmodel = model;
	initService();
}

void RsGisRpcMaster::initService()
{
	ExtensionManager* em = ExtensionManager::instance();
	ExtensionSession* es = ExtensionSession::instance();

	QObject* obj = es->getObject("XmlRpcMaster");
	XmlRpcMaster* rpcMaster = (XmlRpcMaster*)(obj);

	connect(rpcMaster, SIGNAL(TaskArrange(QVariantList, quint16)), taskmodel, SLOT(TaskInfoChange(QVariantList, quint16)));

	rpcMaster->initClient2Master(quint16(8080));
	rpcMaster->initMaster2Server(quint16(8084));
	//rpcMaster->initMaster2Server(quint16(8083));

	rpcMaster->addService("Master.CallServer", this, "CallServer");
	rpcMaster->addService("Master.CallforResult", this, "CallforResult");
}

void RsGisRpcMaster::GetService()
{
	ExtensionManager* em = ExtensionManager::instance();
	ExtensionSession* es = ExtensionSession::instance();

	QObject* obj = es->getObject("XmlRpcMaster");
	XmlRpcMaster* rpcMaster = (XmlRpcMaster*)(obj);

	rpcMaster->getWorkService();
}

QVariant RsGisRpcMaster::CallforResult()
{
	ExtensionManager* em = ExtensionManager::instance();
	ExtensionSession* es = ExtensionSession::instance();

	QObject* obj = es->getObject("XmlRpcMaster");
	XmlRpcMaster* rpcMaster = (XmlRpcMaster*)(obj);

	return rpcMaster->MasterArg;
}

//QVariantList RsGisRpcMaster::CallServer(QVariantList ArgList)
//{
//	ExtensionManager* em = ExtensionManager::instance();
//	ExtensionSession* es = ExtensionSession::instance();
//
//	QObject* obj = es->getObject("XmlRpcMaster");
//	XmlRpcMaster* rpcMaster = (XmlRpcMaster*)(obj);
//	
//	QString sMethod = ArgList[1].toString() + "." + ArgList[2].toString();
//
//	QVariantList ResultList;
//	
//	if (!rpcMaster->askWorkService(sMethod))
//	{
//		ResultList << "Do Not Have This Service!";
//		qDebug() << "Do Not Have This Service!";
//		return ResultList;
//	}
//
//	iTaskNum++;
//
//	QString sTaskNum = QString::number(iTaskNum);
//
//	ArgList.prepend(QVariant(sTaskNum));
//
//	int iPriorityNum = ArgList[4].toInt();
//	//ArgList.removeAt(4);
//	QString sPriorityNum = QString::number(iPriorityNum);
//
//	quint16 iPortClient = (quint16)ArgList[1].toInt();
//	quint16 iPortServer = rpcMaster->arrangeWorkService(sMethod, ArgList, iPriorityNum);
//
//	QString sPortClient = QString::number(iPortClient);
//	QString sPortServer = QString::number(iPortServer);
//
//	if (iPortServer == 0)
//	{
//		rpcMaster->AddTaskToDoList(ArgList, iPriorityNum);
//		taskmodel->AddTask(ArgList[0].toString(), ArgList[2].toString(), ArgList[3].toString(), sPortClient, "Waiting", sPriorityNum, "0");
//		ResultList << "Wait for Free Service";
//		qDebug() << "Wait for Free Service";
//		return ResultList;
//	}
//	else
//	{
//		taskmodel->AddTask(ArgList[0].toString(), ArgList[2].toString(), ArgList[3].toString(), sPortClient, sPortServer, sPriorityNum, "0");
//		ResultList << "Running Task";
//		qDebug() << "Running Task";
//		return ResultList;
//	}
//}

QVariantList RsGisRpcMaster::CallServer(QVariantList ArgList)
{
	ExtensionManager* em = ExtensionManager::instance();
	ExtensionSession* es = ExtensionSession::instance();

	QObject* obj = es->getObject("XmlRpcMaster");
	XmlRpcMaster* rpcMaster = (XmlRpcMaster*)(obj);

	QList<QVariant> SysParaList = ArgList[0].value<QList<QVariant>>();
	QList<QVariant> AlgParaList = ArgList[1].value<QList<QVariant>>();

	QString sMethod = SysParaList[0].toString() + "." + SysParaList[1].toString();

	QVariantList ResultList;
	if (!rpcMaster->askWorkService(sMethod))
	{
		ResultList << "Do Not Have This Service!";
		qDebug() << "Do Not Have This Service!";
		return ResultList;
	}

	iTaskNum++;
	QString sTaskNum = QString::number(iTaskNum);
	SysParaList << QVariant(sTaskNum);

	int iPriorityNum = SysParaList[2].toInt();
	QString sPriorityNum = QString::number(iPriorityNum);

	ArgList.clear();
	ArgList << (QVariant)SysParaList;
	ArgList << (QVariant)AlgParaList;

	quint16 iPortClient = (quint16)SysParaList[3].toInt();
	quint16 iPortServer = rpcMaster->arrangeWorkService(ArgList);

	QString sPortClient = QString::number(iPortClient);
	QString sPortServer = QString::number(iPortServer);

	if (iPortServer == 0)
	{
		rpcMaster->AddTaskToDoList(ArgList, iPriorityNum);
		taskmodel->AddTask(SysParaList[4].toString(), SysParaList[0].toString(), SysParaList[1].toString(), SysParaList[3].toString(), "Waiting", SysParaList[2].toString(), "0");
		ResultList << "Wait for Free Service";
		qDebug() << "Wait for Free Service";
		return ResultList;
	}
	else
	{
		taskmodel->AddTask(SysParaList[4].toString(), SysParaList[0].toString(), SysParaList[1].toString(), sPortClient, sPortServer, sPriorityNum, "0");
		ResultList << "Running Task";
		qDebug() << "Running Task";
		return ResultList;
	}
}
